package com.vcredit.creditcat.time.creditassure;

import java.util.List;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.alibaba.dubbo.common.threadpool.RequestLocalThread;
import com.vcredit.bird.service.foreignTradeCompliance.ForeignTradeComplianceService;
import com.vcredit.bird.vo.foreignTradeCompliance.ReportStateResponse;
import com.vcredit.creditcat.manager.CreditAssureTimaManager;
import com.vcredit.creditcat.model.CreditAssure;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.time.creditassure.CreditAssureApplyTime.CrawleThread;
import com.vcredit.framework.quartz.QuartzJob;

@QuartzJob(name = "creditAssurePreAuditTime", cron = "0 0/10 * * * ?")
public class CreditAssurePreAuditTime extends QuartzJobBean{

	private Logger logger = LoggerFactory.getLogger(CreditAssurePreAuditTime.class);

	@Autowired
	private  CreditAssureTimaManager creditAssureTimaManager;
	@Autowired
	private ForeignTradeComplianceService foreignTradeComplianceService;
	@Autowired
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;
	
	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		RequestLocalThread.create();
 		List<CreditAssure> caList = creditAssureTimaManager.findToPreAudit();
		if (caList == null || caList.isEmpty()){
			return;
		}
		for (CreditAssure ca : caList) {
			threadPoolTaskExecutor.execute(new CrawleThread(ca));		
		}
	}
	
class CrawleThread implements Runnable {
		
		private Logger CrawleLOG = LoggerFactory.getLogger(CrawleThread.class);
		
		private CreditAssure obj;
		
		public CrawleThread(CreditAssure obj) {
			super();
			this.obj = obj;
		}

		public void run() {
			try {
				Cust cust = obj.getCust();
				if(cust==null){
					return;
				}
				creditAssureTimaManager.preAudit(obj);

			} catch (Exception e) {
				CrawleLOG.error("CreditAssureApplyTime error:", e);
			}
		}
	}

}
